<template>
  <el-dialog v-model="addOrEditVisible" title="用户业绩核销"
             :before-close="closeEditDialog"
             width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
             label-width="160px" class="demo-ruleForm"
             :size="formSize" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="" prop="id">
            <el-input v-model="ruleForm.id"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="核销时间" prop="verifyTime">
            <el-input v-model="ruleForm.verifyTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户名称" prop="adminId">
            <el-input v-model="ruleForm.adminId"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户等级" prop="adminLevel">
            <el-input v-model="ruleForm.adminLevel"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="总单子数量" prop="orderCount">
            <el-input v-model="ruleForm.orderCount"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="自己的单子" prop="ownerOrderCount">
            <el-input v-model="ruleForm.ownerOrderCount"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="总金额" prop="totalPrice">
            <el-input v-model="ruleForm.totalPrice"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="自己单子金额" prop="ownerOrderPrice">
            <el-input v-model="ruleForm.ownerOrderPrice"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="核销总金额" prop="totalVerifPrice">
            <el-input v-model="ruleForm.totalVerifPrice"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="自己单子核销金额" prop="totalOwnerVerifPrice">
            <el-input v-model="ruleForm.totalOwnerVerifPrice"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="提现手续费" prop="wechatPer">
            <el-input v-model="ruleForm.wechatPer"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="提现金额" prop="wechatPrice">
            <el-input v-model="ruleForm.wechatPrice"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="审核单子核销状态 0-待审核 1-审核通过 2-审核不通过"
                        prop="examineVerify">
            <el-input v-model="ruleForm.examineVerify"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="提现状态 0-待提现 1-已提现 2-提现失败" prop="tixianFlag">
            <el-input v-model="ruleForm.tixianFlag"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="状态 0-停用 1-启用 2-备用" prop="statusFlag">
            <el-input v-model="ruleForm.statusFlag"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="是否删除 0-否 1-是" prop="delFlag">
            <el-input v-model="ruleForm.delFlag"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="创建人" prop="createUser">
            <el-input v-model="ruleForm.createUser"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="创建时间" prop="createTime">
            <el-input v-model="ruleForm.createTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="修改时间" prop="updateTime">
            <el-input v-model="ruleForm.updateTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="ruleForm.remark"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
         <span class="dialog-footer">
           <el-button @click="closeEditDialog">关闭</el-button>
           <el-button type="primary"
                      @click="submitForm(ruleFormRef)">提交</el-button>
         </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage} from "element-plus";
import router from "@/router";
import {
  SysAdminVerify
} from '@/apis/model/order/SysAdminVerifyModel';
import {
  insertSysAdminVerify,
  updateSysAdminVerify
} from '@/apis/http/order/SysAdminVerifyHttp';

interface Prop {
  addOrEditVisible: boolean,
  ruleForm: SysAdminVerify,
  addDataType: string
}

// 刷新list页面
const refushList = ref(false);

// 传递过来的参数
const props = defineProps<Prop>();

const emit = defineEmits(["closeEditDialog"]);

// 关闭按钮
const closeEditDialog = () => {
  emit("closeEditDialog", {
    addOrEditVisible: !props.addOrEditVisible,
    refushList: refushList.value
  })
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  id: [
    {required: true, message: '请输入', trigger: 'blur'}
  ],
  verifyTime: [
    {required: true, message: '请输入核销时间', trigger: 'blur'}
  ],
  adminId: [
    {required: true, message: '请输入用户名称', trigger: 'blur'}
  ],
  adminLevel: [
    {required: true, message: '请输入用户等级', trigger: 'blur'}
  ],
  orderCount: [
    {required: true, message: '请输入总单子数量', trigger: 'blur'}
  ],
  ownerOrderCount: [
    {required: true, message: '请输入自己的单子', trigger: 'blur'}
  ],

  totalPrice: [
    {required: true, message: '请输入总金额', trigger: 'blur'}
  ],
  ownerOrderPrice: [
    {required: true, message: '请输入自己单子金额', trigger: 'blur'}
  ],
  totalVerifPrice: [
    {required: true, message: '请输入核销总金额', trigger: 'blur'}
  ],
  totalOwnerVerifPrice: [
    {required: true, message: '请输入自己单子核销金额', trigger: 'blur'}
  ],

  wechatPer: [
    {required: true, message: '请输入提现手续费', trigger: 'blur'}
  ],
  wechatPrice: [
    {required: true, message: '请输入提现金额', trigger: 'blur'}
  ],
  examineVerify: [
    {
      required: true,
      message: '请输入审核单子核销状态 0-待审核 1-审核通过 2-审核不通过',
      trigger: 'blur'
    }
  ],
  tixianFlag: [
    {required: true, message: '请输入提现状态 0-待提现 1-已提现 2-提现失败', trigger: 'blur'}
  ],
  statusFlag: [
    {required: true, message: '请输入状态 0-停用 1-启用 2-备用', trigger: 'blur'}
  ],
  delFlag: [
    {required: true, message: '请输入是否删除 0-否 1-是', trigger: 'blur'}
  ],
  createUser: [
    {required: true, message: '请输入创建人', trigger: 'blur'}
  ],
  createTime: [
    {required: true, message: '请输入创建时间', trigger: 'blur'}
  ],
  updateTime: [
    {required: true, message: '请输入修改时间', trigger: 'blur'}
  ],
  remark: [
    {required: true, message: '请输入备注', trigger: 'blur'}
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 新增
      if (props.addDataType === 'add') {
        insertSysAdminVerify(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '创建成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('创建失败！')
          }
        })
      } else if (props.addDataType === 'edit') {
        updateSysAdminVerify(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '更新成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('更新失败！')
          }
        })
      }

    } else {
      ElMessage.error('请完善信息！')
    }
  })
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
